import os

numInput = 0
numOutput = 0

folderName = raw_input("Enter the folder containing the data files to convert ... ")

files = os.listdir(folderName)
for each in files:
	if(os.path.isdir(each)):
		files.remove(each)
		
fileStr = ""
for eachFile in files:
	fileStr += eachFile + ", "
	
print("Converting "+fileStr)

if(os.path.isdir(folderName+"/Converted/") == False):
	os.mkdir(folderName+"/Converted/")

for FileName in files: 
	FileObj = open(folderName+"/"+FileName, "r")
	rawData = FileObj.readlines()
	FileObj.close()

	data = []

	dataPairs = 0

	for eachLine in rawData:
		data.append(eachLine.strip(" \n\t").split(","))
		dataPairs += 1

	numOutput = 1
		
	for each in data:
		numInput = len(each) - 1

	newFile = open(folderName+"/Converted/fann_"+FileName, "w")
	newFile.write(str(dataPairs)+" "+str(numInput)+" "+str(numOutput)+"\n")

	lineCount = 0
	for each in data:
		output = each[-1]
		del each[-1]
		inputStr = " ".join(each)
		newFile.write(inputStr+"\n")
		newFile.write(output +"\n")
		lineCount+=1
		print("Converting line "+str(lineCount)+" of file "+FileName)
		
	newFile.close()
